package UILayer;

import CtrLayer.CustomerCtr;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import javax.swing.table.*;

public class Customers extends JInternalFrame
{
	private JTextField text_field;
	private CustomerCtr customers;
	private JTable table;
	private Object[] column_names = { "Client Number", "Name", "Phone", "Address", "Postal code" };
	
	public Customers()
	{
		super("Manage Customers", true, true, true, true);
		getContentPane().setForeground(new Color(255, 153, 0));
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			public void internalFrameActivated(InternalFrameEvent e)
			{
				refill_table();
			}
		});
		customers = new CustomerCtr();
		fill_frame();
	}

	private void refill_table()
	{
		Object[][] filling = customers.display_all_customers();
		table.setModel(new DefaultTableModel(filling, column_names));

	}

	private void make_table()
	{

		Object[][] filling = customers.display_all_customers();
		JPanel ctr_btn_panel = new JPanel();
		ctr_btn_panel.setBackground(new Color(255, 153, 0));
		ctr_btn_panel.setBorder(new EmptyBorder(3, 3, 0, 3));
		
		getContentPane().add(ctr_btn_panel, BorderLayout.SOUTH);
		ctr_btn_panel.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 153, 0));
		ctr_btn_panel.add(panel, BorderLayout.EAST);

		JButton btn_new = new JButton("New");
		panel.add(btn_new);
		btn_new.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				add_customer();
			}
		});

		JButton btn_edit = new JButton("Edit");
		panel.add(btn_edit);
		btn_edit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				edit();
			}
		});

		JButton btn_del = new JButton("Delete");
		panel.add(btn_del);
		btn_del.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				delete();
			}
		});

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(255, 153, 0));
		ctr_btn_panel.add(panel_1, BorderLayout.WEST);

		JButton btn_det = new JButton("Details");
		panel_1.add(btn_det);
		btn_det.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				view();
			}
		});

		JPanel panel_2 = new JPanel();
		getContentPane().add(panel_2, BorderLayout.CENTER);

		table = new JTable()
		{
			public boolean isCellEditable(int row, int column)
			{
				return false;
			}
		};

		table.setForeground(Color.WHITE);
		table.setFont(new Font("Tahoma", Font.PLAIN, 11));
		table.setBackground(new Color(127, 127, 127));
		table.setFillsViewportHeight(true);
		table.setModel(new DefaultTableModel(filling, column_names));

		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setAutoCreateRowSorter(true);
		table.setRowSelectionAllowed(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getTableHeader().setReorderingAllowed(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				if (e.getClickCount() == 2)
				{
					view();
				}
			}
		});
		table.addKeyListener(new KeyAdapter()
		{
			public void keyTyped(KeyEvent e)
			{
				if (e.getKeyChar() == '\u007F')
				{
					delete();
				}
			}
		});

		DefaultTableCellRenderer right_renderer = new DefaultTableCellRenderer();
		right_renderer.setHorizontalAlignment(JLabel.RIGHT);
		table.setDefaultRenderer(String.class, right_renderer);

		JScrollPane scroll_pane = new JScrollPane(table);
		scroll_pane.setBorder(new EmptyBorder(5, 10, 5, 10));
		getContentPane().add(scroll_pane, BorderLayout.CENTER);
	}

	private void make_search(JPanel panel)
	{
		JPanel search_panel = new JPanel();
		search_panel.setBorder(null);
		search_panel.setBackground(new Color(255, 153, 0));
		panel.add(search_panel, BorderLayout.WEST);
		search_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JLabel lblNewLabel = new JLabel("Name/ID");
		search_panel.add(lblNewLabel);

		text_field = new JTextField();
		search_panel.add(text_field);
		text_field.setColumns(20);

		JButton btn_search = new JButton("Search");
		btn_search.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				search();
			}
		});
		search_panel.add(btn_search);

		JButton btn_clear = new JButton("Clear");
		btn_clear.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				refill_table();
				text_field.setText("");
			}
		});
		search_panel.add(btn_clear);

	}

	private void add_customer()
	{
		CustomerManager p = new CustomerManager(null, 0);
		getDesktopPane().add(p);
		p.setVisible(true);
	}

	private void edit()
	{
		int row_index = table.getSelectedRow();
		if (row_index != -1)
		{
			if( table.getModel().getValueAt(row_index, 0) != null)
			{
				CustomerManager p = new CustomerManager((String) table.getModel().getValueAt(row_index, 0), 0);
				getDesktopPane().add(p);
				p.setVisible(true);
			}
		}
	}

	private void view()
	{
		int row_index = table.getSelectedRow();
		if (row_index != -1)
		{
			if( table.getModel().getValueAt(row_index, 0) != null)
			{
				CustomerManager p = new CustomerManager((String) table.getModel().getValueAt(row_index, 0), 1);
				getDesktopPane().add(p);
				p.setVisible(true);
			}
		}
	}

	private void delete()
	{
		int row_index = table.getSelectedRow();
		if (row_index != -1)
		{
			if( table.getModel().getValueAt(row_index, 0) != null)
			{
				if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
						"Are you sure you want to delete this customer?",
						"Delete customer", JOptionPane.YES_NO_OPTION))
				{
					customers.delete_customer((String) table.getModel()
							.getValueAt(row_index, 0));
					refill_table();
				}
			}
		}

	}

	private void search()
	{
		Object[][] fill = new Object[1][5];
		String info = text_field.getText();
		if (is_numeric(info))
		{
			if (customers.existing_customer(info))
			{
			fill[0] = customers.find_customer_by_id(info);
			}
			else 
			{
				fill[0]=null;
			}
		}
		else
		{
			if (customers.find_customer_by_name(info)!=null)
			{
				fill[0] = customers.find_customer_by_name(info);
			}
			else 
			{
				fill[0]=null;
			}
		}
		table.setModel(new DefaultTableModel(fill, column_names));
	}

	private void fill_frame()
	{
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout(0, 0));
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 153, 0));
		panel.setBorder(null);
		getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		make_search(panel);
		make_table();
	}
	

	private boolean is_numeric(String checker)
	{
		try
		{
			int i = Integer.parseInt(checker);
		}
		catch (NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}

}
